<template>
  <div class="app-container">
    <el-card>
      <div class="btn_wrapper">
        <span>说明：目前支持学科和关键字条件筛选</span>
        <el-button type="success" icon="el-icon-edit" size="small" @click="jumpAdd">新增试题</el-button>
      </div>

      <!-- 搜索表单 -->
      <el-form ref="form" :model="searchForm" label-width="80px">
        <el-row :gutter="10">
          <el-col :span="6">
            <el-form-item label="学科">
              <el-select v-model="searchForm.subjectID" placeholder="请选择" @change="getCatalogAndTagsList">
                <el-option v-for="item in subjects" :key="item.id" :label="item.subjectName" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="二级目录">
              <el-select v-model="searchForm.catalogID" placeholder="请选择">
                <el-option v-for="item in catalogs" :key="item.id" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="标签">
              <el-select v-model="searchForm.tags" placeholder="请选择">
                <el-option v-for="item in tagsList" :key="item.value" :label="item.label" :value="item.label"> </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="关键字">
              <el-input v-model="searchForm.keyword"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="6">
            <el-form-item label="试题类型">
              <el-select v-model="searchForm.questionType" placeholder="请选择">
                <el-option v-for="item in questionType" :key="item.value" :label="item.label" :value="item.label"> </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="难度">
              <el-select v-model="searchForm.difficulty" placeholder="请选择">
                <el-option v-for="item in difficulty" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="方向">
              <el-select v-model="searchForm.direction" placeholder="请选择">
                <el-option v-for="(item, index) in direction" :key="index" :label="item" :value="item"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="录入人">
              <el-select v-model="searchForm.ceartorID" placeholder="请选择">
                <el-option v-for="(item, index) in users" :key="index" :label="item.username" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="6">
            <el-form-item label="题目备注">
              <el-input v-model="searchForm.remark"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="企业简称">
              <el-input v-model="searchForm.shortName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="城市">
              <el-select v-model="searchForm.province" style="width: 96px; margin-right:10px ;" placeholder="请选择" @change="choseProvince">
                <el-option v-for="(item, index) in provinceList" :key="index" :label="item" :value="item"></el-option>
              </el-select>
              <el-select v-model="searchForm.city" style="width: 96px;" placeholder="请选择">
                <el-option v-for="(item, index) in cityList" :key="index" :label="item" :value="item"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item style="text-align: right;">
              <el-button size="small" @click="clearFrom">清除</el-button>
              <el-button type="primary" size="small" @click="searchQuestion">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!-- tabs标签页 -->
      <el-tabs type="card" v-model="activeName"  @tab-click="handleClick">
        <el-tab-pane label="全部" name="first"><QuestionTable :choiceList="choiceList" :total="total" @delete="handleDelete" @check="handleCheck" @update="handleShelf"></QuestionTable></el-tab-pane>

        <el-tab-pane label="待审核" name="second" ><QuestionTable :choiceList="awaitData" :total="total" @delete="handleDelete"  @check="handleCheck"  @update="handleShelf"></QuestionTable></el-tab-pane>

        <el-tab-pane label="已审核" name="third"><QuestionTable :choiceList="passData" :total="total" @delete="handleDelete"  @check="handleCheck"  @update="handleShelf"></QuestionTable></el-tab-pane>

        <el-tab-pane label="已拒绝" name="fourth"><QuestionTable :choiceList="refuseData" :total="total" @delete="handleDelete"  @check="handleCheck"  @update="handleShelf"></QuestionTable></el-tab-pane>
      </el-tabs>

   <el-pagination
        style="text-align: right;"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="pagesize"
        layout=" prev, pager, next,sizes,jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { provinces, citys } from '@/api/hmmm/citys' // 省市列表
import { list as subjectList } from '@/api/hmmm/subjects' // 学科列表
import { simple } from '@/api/hmmm/directorys' // 目录列表
import { simple as tagsList } from '@/api/hmmm/tags' // 标签列表
import { list as companysList } from '@/api/hmmm/companys' // 公司列表
import { simple as usersList } from '@/api/base/users' // 用户列表
import { difficulty, questionType, direction } from '@/api/hmmm/constants' // 难度、题型、方向
import { choice } from '@/api/hmmm/questions' // 题目列表
import { parseTime, html2Text } from '@/utils/index'
import QuestionTable from '../components/question-table'
export default {
  components: { QuestionTable },
  data () {
    return {
      page: 1,
      pagesize: 5,
      activeName: 'first',
      state: '',
      total: 0,
      flag: false,
      choiceList: [], // 全部精选列表
      awaitData: [], // 待审核列表
      passData: [], // 已审核列表
      refuseData: [], // 已拒绝列表
      searchForm: {
        subjectID: '', // 学科
        difficulty: '', // 难度
        questionType: '', // 试题类型
        tags: '', // 标签名称
        keyword: '', // 关键字
        direction: '', // 方向
        remark: '', // 题目备注
        shortName: '', // 企业简称
        city: '', // 城市
        province: '', // 省份
        ceartorID: '', // 录入人
        catalogID: '' // 目录
      },
      type: 'first',
      subjects: [], // 学科列表
      catalogs: [], // 目录列表
      tagsList: [], // 标签列表
      companys: [], // 企业列表
      cityList: [], // 城市列表
      provinceList: provinces(), // 省份列表
      difficulty,
      questionType,
      direction,
      users: [] // 录入人列表
    }
  },
  created () {
    this.getChoiceList()
    this.getSubjectList()
    this.getUsersList()
  },
  methods: {
    // 获取学科列表
    async getSubjectList () {
      const res = await subjectList()
      this.subjects = res.data.items
    },
    // 获取录入人列表
    async getUsersList () {
      const res = await usersList()
      this.users = res.data
    },
    // 获取目录和标签列表
    async getCatalogAndTagsList () {
      const res = await simple({ subjectID: this.searchForm.subjectID })
      this.catalogs = res.data
      const res1 = await tagsList({ subjectID: this.searchForm.subjectID })
      this.tagsList = res1.data
    },
    // 获取企业列表
    async getCompanyList () {
      const res = await companysList()
      this.companys = res.data.items
    },
    // 获取城市列表
    choseProvince (val) {
      this.cityList = citys(val)
    },
    // 清空表单
    clearFrom () {
      for (const key in this.searchForm) {
        this.searchForm[key] = ''
      }
    },
    handleCurrentChange (val) {
      this.page = val
      this.handleRender()
    },
    handleSizeChange (val) {
      this.pagesize = val
      this.handleRender()
    },
    // 搜索
    async  searchQuestion () {
      const search = {
        ...this.searchForm,
        page: this.page,
        pagesize: this.pagesize
      }
      if (this.searchForm.subjectID || this.searchForm.keyword) {
        const res = await choice(search)
        this.choiceList = res.data.items
        this.total = res.data.counts
        this.handleData(this.choiceList)
      } else {
        await this.getChoiceList()
      }
    },
    // 获取精选题库列表
    async getChoiceList () {
      const res = await choice({ page: this.page, pagesize: this.pagesize })
      this.choiceList = res.data.items
      this.total = res.data.counts
      this.handleData(this.choiceList)
    },
    // 获取未审核列表
    async getAwaitList () {
      const chkState = 0
      const res = await choice({ page: this.page, pagesize: this.pagesize, chkState: chkState })
      console.log(res)
      this.awaitData = res.data.items
      this.total = res.data.counts
      this.handleData(this.awaitData)
    },
    // 获取已审核列表
    async getPassList () {
      const chkState = 1
      const res = await choice({ page: this.page, pagesize: this.pagesize, chkState: chkState })
      this.passData = res.data.items
      this.total = res.data.counts
      this.handleData(this.passData)
    },
    // 获取已拒绝列表
    async getRefuseList () {
      const chkState = 2
      const res = await choice({ page: this.page, pagesize: this.pagesize, chkState: chkState })
      this.refuseData = res.data.items
      this.total = res.data.counts
      this.handleData(this.refuseData)
    },
    // 处理数据
    async handleData (data) {
      data.forEach(item => {
        item.addDate = parseTime(item.addDate)
      })
      data.forEach(item => {
        item.question = html2Text(item.question)
      })
      // const res1 = await choice()
      // console.log(res1)
      // this.awaitData = this.choiceList.filter(item => item.chkState === 0)
      // this.passData = this.choiceList.filter(item => item.chkState === 1)
      // this.refuseData = this.choiceList.filter(item => item.chkState === 2)
    },

    // 跳转
    jumpAdd () {
      this.$router.push({ name: 'questions-new' })
    },
    // 跳转发送请求
    handleClick (tab) {
      this.page = 1
      if (tab.name === 'first') {
        this.type = 'first'
        this.getChoiceList()
      } else if (tab.name === 'second') {
        this.type = 'second'
        this.getAwaitList()
      } else if (tab.name === 'third') {
        this.type = 'third'
        this.getPassList()
      } else if (tab.name === 'fourth') {
        this.type = 'fourth'
        this.getRefuseList()
      }
    },
    // 删除后渲染数据
    handleDelete () {
      this.handleRender()
    },
    // 渲染数据
    handleRender () {
      if (this.type === 'first') {
        this.getChoiceList()
      } else if (this.type === 'second') {
        this.getAwaitList()
      } else if (this.type === 'third') {
        this.getPassList()
      } else if (this.type === 'fourth') {
        this.getRefuseList()
      }
    },
    // 审核后渲染数据
    handleCheck () {
      if (this.type === 'first') {
        this.getChoiceList()
      } else if (this.type === 'second') {
        this.getAwaitList()
      }
    },
    // 上下架后渲染数据
    handleShelf () {
      this.handleRender()
    }
  }
}
</script>

<style scoped lang="scss">
.btn_wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
  span {
    font-size: 12px;
    color: pink;
  }
}
::v-deep .el-pagination__jump {
  margin-left: 0;
}
</style>
